import * as fse from "fs-extra";
import * as path from "path"

//存储目录
const DIST_FOLDER_PATH = path.join("C:/Users/蓝士324/Desktop/role", "uploadFiles")
// const filepath =
// #最大体积5M
const MAX_SIZE = 1024 * 1024 * 1024 * 5

interface ISaveFileParams {
    name: string
    type: string
    size: number
    filePath: string
}

async function saveFile(params: ISaveFileParams) {
    const { name, type, size, filePath } = params
    if (size > MAX_SIZE) {
        //删除文件
        await fse.remove(filePath)
        // return new ErrorModel(uploadFileSizeFailInfo)
        return {
            msg: "上传失败，文件太大",
            code: 201
        }
    }

    //移动文件
    const fileName = Date.now() + "." + name //防止重名
    const distFilePath = path.join(DIST_FOLDER_PATH, fileName)  //目的地
  const res =   await fse.move(filePath, distFilePath);
  console.log("res", distFilePath);
  

    //返回信息 132.jpg
    return fileName
    // return new SuccessModel({
    //     url: "/" + fileName
    // })

}


export {
    saveFile
}